package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacades "github.com/hulutech-web/http_result"
	"goravel/app/models"
)

type SearchfilterController struct {
	//Dependent services
}

func NewSearchfilterController() *SearchfilterController {
	return &SearchfilterController{
		//Inject services
	}
}

func (r *SearchfilterController) Index(ctx http.Context) http.Response {
	var searchfilters []models.SearchFilter
	queries := ctx.Request().Queries()
	result, _ := httpfacades.NewResult(ctx).SearchByParams(queries).ResultPagination(&searchfilters)
	return result
}

func (r *SearchfilterController) Show(ctx http.Context) http.Response {
	id := ctx.Request().RouteInt("id")
	var searchfilter models.SearchFilter
	facades.Orm().Query().Model(&models.SearchFilter{}).Where("id", id).First(&searchfilter)
	return httpfacades.NewResult(ctx).Success("", searchfilter)
}

func (r *SearchfilterController) Store(ctx http.Context) http.Response {
	var searchfilter models.SearchFilter
	if err := ctx.Request().Bind(&searchfilter); err != nil {
		return httpfacades.NewResult(ctx).Error(http.StatusInternalServerError, err.Error(), nil)
	}
	if err := facades.Orm().Query().Model(&models.SearchFilter{}).Create(&searchfilter); err != nil {
		return httpfacades.NewResult(ctx).Error(http.StatusInternalServerError, "创建失败", nil)
	}

	return httpfacades.NewResult(ctx).Success("创建成功", nil)
}

func (r *SearchfilterController) Update(ctx http.Context) http.Response {
	var searchfilter models.SearchFilter

	id := ctx.Request().RouteInt("id")
	if err := ctx.Request().Bind(&searchfilter); err != nil {
		return httpfacades.NewResult(ctx).Error(http.StatusInternalServerError, err.Error(), nil)
	}
	_, err := facades.Orm().Query().Model(&models.SearchFilter{}).Where("id", id).Update(searchfilter)
	if err != nil {
		return httpfacades.NewResult(ctx).Error(http.StatusInternalServerError, "更新失败", nil)
	}
	return httpfacades.NewResult(ctx).Success("更新成功", nil)
}

func (r *SearchfilterController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().RouteInt("id")
	facades.Orm().Query().Model(&models.SearchFilter{}).Where("id", id).Delete(id)
	return httpfacades.NewResult(ctx).Success("删除成功", nil)
}
